package com.hustcad.plm.pdm.user.service.impl;



import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.common.controller.TyplmObjsRenameController;
import com.hustcad.plm.pdm.common.model.dto.ObjRenameDTO;
import com.hustcad.plm.pdm.common.model.vo.ObjsRenameConditionVO;
import com.hustcad.plm.pdm.user.mapper.ErpBomMapper;
import com.hustcad.plm.pdm.user.mapper.OptionMapper;
import com.hustcad.plm.pdm.user.service.ErpBomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ErpBomServiceImpl implements ErpBomService {

    @Autowired
    ErpBomMapper erpBomMapper;

    @Override
    public Integer insertErpBom(Map<String, Object> map) {
        return erpBomMapper.insertErpBom(map);
    }

    @Override
    public List<Map<String, Object>> getErpBom(Map<String, Object> map) {
        return erpBomMapper.getErpBom(map);
    }

    @Override
    public Integer updateErpBom(List<Map<String, Object>> list) {
        return erpBomMapper.updateErpBom(list);
    }

    @Override
    public List<Map<String, Object>> getAllDoc(Map<String, Object> map) {
        return erpBomMapper.getAllDoc(map);
    }

    @Override
    public Integer updateDocFile(Map<String, Object> map) {

        List<Map<String, Object>> valueList=getAllCadDocIbaByMasteredOid(map);

        TyplmObjsRenameController typlmObjsRenameController= SpringUtil.getBean(TyplmObjsRenameController.class);
        for(int i=0;i<valueList.size();i++){
            ObjsRenameConditionVO objsRenameConditionVO=new ObjsRenameConditionVO();
            objsRenameConditionVO.setExcludeException(false);

            List<ObjRenameDTO> objRenameDTOS=new ArrayList<>();
            ObjRenameDTO objRenameDTO=new ObjRenameDTO();
            objRenameDTO.setOid(((BigDecimal)valueList.get(i).get("objbaseoid")).toString());
            objRenameDTO.setNumber("");
            objRenameDTO.setOtype("ty.inteplm.cad.CTyCADDoc");
            objRenameDTO.setName((String) map.get("update"));
            objRenameDTOS.add(objRenameDTO);
            objsRenameConditionVO.setObjRenameDTOS(objRenameDTOS);
            typlmObjsRenameController.renameIdentityAndName(objsRenameConditionVO);
        }

        return erpBomMapper.updateDocFile(map);
    }

    @Override
    public List<Map<String, Object>> getPartMaster(Map<String, Object> map) {
        return erpBomMapper.getPartMaster(map);
    }

    @Override
    public List<Map<String, Object>> getDocMaster(Map<String, Object> map) {
        return erpBomMapper.getDocMaster(map);
    }

    @Override
    public List<Map<String, Object>> selectFiguration(Map<String, Object> map) {
        return erpBomMapper.selectFiguration(map);
    }

    @Override
    public Integer saveFiguration(Map<String, String> map) {
        return erpBomMapper.saveFiguration(map);
    }

    @Override
    public Integer updateFiguration(Map<String, String> map) {
        return erpBomMapper.updateFiguration(map);
    }

    @Override
    public List<Map<String, Object>> getConfigurationType(Map<String, Object> map) {
        return erpBomMapper.getConfigurationType(map);
    }

    @Override
    public Integer saveFeiShuUser(Map<String, String> map) {
        return erpBomMapper.saveFeiShuUser(map);
    }

    @Override
    public List<Map<String, Object>> getFeiShuUser(Map<String, Object> map) {
        return erpBomMapper.getFeiShuUser(map);
    }

    @Override
    public List<Map<String, Object>> getAllCadDocByMasteredOid(Map<String, Object> map) {
        return erpBomMapper.getAllCadDocIbaByMasteredOid(map);
    }

    @Override
    public List<Map<String, Object>> getAllCadDocIbaByMasteredOid(Map<String, Object> map) {
        return erpBomMapper.getAllCadDocIbaByMasteredOid(map);
    }

    public static BigInteger generateUniqueId() {
        long timestamp = Instant.now().toEpochMilli();
        long sequence = System.nanoTime() % 1000000;

        String idStr = timestamp + "" + 1 + "" + sequence;
        return new BigInteger(idStr);
    }
}
